package com.jt.www.remote;

import com.google.gson.JsonObject;
import com.jt.www.domain.qo.EndorsQueryParam;
import com.jt.www.domain.qo.EndorsQueryResp;
import com.jt.www.domain.qo.EndorsementsEduQueryVO;
import com.jt.www.domain.vo.EduPolicyBean;
import com.jt.www.domain.vo.EduPolicyVO;
import com.jt.www.domain.vo.PolQueryForProDetailVO;
import com.jt.www.model.correct.vo.EndorsementRectifyVO;
import com.jt.www.model.pol.UploadEndorsementCheckFile;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.reps.GenericResponse;
import com.jt.www.model.vo.endorse.EduQueryEndorsementTypeAndCountInfo;
import com.jt.www.model.vo.endorse.EndorseStatusVo;
import com.jt.www.model.vo.sdCheck.EndorsementsInfo;
import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.Map;

@Component
@FeignClient(value = "pol-svc", url = "${remote.pol_svc_url}",
        configuration = {FeignConfiguration.class},
        fallbackFactory = PolClient.FeignClientFallbackFactory.class)
public interface PolClient {

    @GetMapping(value = "/education/queryEndorsementStatusByOrderCode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericListResponse<EndorseStatusVo>> queryEndorsementStatusByOrderCode(
            @RequestParam(value = "orderCode") String orderCode) throws Exception;


    @GetMapping(value = "/endorsement/v1/validateSubmitEndorsement", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericListResponse<EndorsementsInfo>> validateSubmitEndorsement(
            @RequestParam(value = "policyNo") String policyNo, @RequestParam(value = "channelCode") String channelCode) throws Exception;

    @GetMapping(value = "/education/v1/queryEduEndorsemenstInfo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericListResponse<EduQueryEndorsementTypeAndCountInfo>> queryEduEndorsemenstInfo(@RequestParam(value = "policyNo") String policyNo) throws Exception;

    //根据批单申请号查询批单信息
    @GetMapping(value = "/education/v1/queryEndorsementDetailByApplyNo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    //ResponseEntity<GenericDataResponse<EduQueryEndorsementDetailVo>> queryEndorsementDetailByApplyNo(@RequestParam(value = "applyNo") String applyNo) throws Exception;
    String queryEndorsementDetailByApplyNo(@RequestParam(value = "applyNo") String applyNo) throws Exception;


    @PostMapping(value = "education/v1/queryEduEndorsementsInfoList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<EduPolicyVO> queryEduEndorsementsInfoList(@RequestBody EduPolicyBean eduPolicyBean) throws Exception;

    @PostMapping(value = "education/v1/queryEduEndorsementsInfoList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericListResponse<EduPolicyVO> queryEduEndorsementsInfoList(@RequestBody EndorsementsEduQueryVO endorsementsEduQueryVO) throws Exception;

    /*
     *  查询批单确认列表接口
     * @Author xq.wu
     * @Date 12:43 2020/4/21
     * @Param [endorsementsEduQueryVO]
     * @return com.jt.www.model.reps.GenericListResponse<com.jt.www.domain.vo.EduPolicyVO>
     **/
    @PostMapping(value = "endorsement/v1/endorsementConformList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericListResponse<EndorsQueryResp>> endorsementConformList(@RequestBody EndorsQueryParam endorsQueryParam) throws Exception;


    //绑定电子批单
    @PostMapping(value = "/endorsement/v1/uploadEndorsementCheckFiles", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseEntity<GenericListResponse<JsonObject>> uploadEndorsementCheckFiles(@RequestBody UploadEndorsementCheckFile uploadEndorsementCheckFile) throws Exception;

    /**
     * 责任险保单详情查询
     *
     * @param policyNo
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/liabilitySvc/v1/queryLiabilityPolDetail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericDataResponse<PolQueryForProDetailVO> queryLiabilityPolDetail(@RequestParam(value = "policyNo") String policyNo) throws Exception;

    /**
     * 提交批单
     *
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/endorsement/submitEndors", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "application/json;charset=UTF-8")
    GenericDataResponse submitEndors(@RequestBody @Valid String json) throws Exception;

    /*
     *  批单冲正
     * @Author xq.wu
     * @Date  2020/6/15
     * @Param [rectifyUpdateQo]
     * @return org.springframework.http.ResponseEntity<com.jt.www.model.reps.GenericResponse>
     **/
    @PostMapping(value = "/liabilitySvc/v1/endorsementRectify", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    ResponseEntity<GenericResponse> endorsementRectify(@RequestBody EndorsementRectifyVO rectifyUpdateQo);

    /**
     * 校验渠道批单号唯一性
     *
     * @param endorsementsNo
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/liabilitySvc/v1/validEndorsementsNoUniq", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    GenericDataResponse<Map<String, Integer>> validEndorsementsNoUniq(@RequestParam(value = "endorsementsNo") String endorsementsNo, @RequestParam(value = "channelCode") String channelCode) throws Exception;


    @Component
    class FeignClientFallbackFactory implements FallbackFactory<PolClient> {

        public static final Logger log = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

        @Override
        public PolClient create(Throwable cause) {
            return new PolClient() {

                @Override
                public ResponseEntity<GenericListResponse<EndorseStatusVo>> queryEndorsementStatusByOrderCode(String orderCode) {
                    log.error("调用保单中心查询出单状态失败", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericListResponse<EndorsementsInfo>> validateSubmitEndorsement(
                        @RequestParam(value = "policyNo") String policyNo, @RequestParam(value = "channelCode") String channelCode) throws Exception {

                    log.error("调用批单中心查询批单状态失败", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericListResponse<EduQueryEndorsementTypeAndCountInfo>> queryEduEndorsemenstInfo(String policyNo) throws Exception {
                    log.error("调用批单中心查询批单记录失败", cause);
                    return null;
                }

                //根据批单申请号查询批单信息
                @Override
                public String queryEndorsementDetailByApplyNo(@RequestParam(value = "applyNo") String applyNo) throws Exception {
                    log.error("调用批单中心根据批单申请号查询批单信息失败", cause);
                    return null;
                }

                @Override
                public GenericListResponse<EduPolicyVO> queryEduEndorsementsInfoList(EduPolicyBean eduPolicyBean) throws Exception {
                    return null;
                }

                @Override
                public GenericListResponse<EduPolicyVO> queryEduEndorsementsInfoList(EndorsementsEduQueryVO endorsementsEduQueryVO) throws Exception {
                    log.error("查询批单列表失败", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericListResponse<EndorsQueryResp>> endorsementConformList(EndorsQueryParam endorsQueryParam) throws Exception {
                    log.error("调用批单中心根据批单申请号查询批单确认信息失败", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericListResponse<JsonObject>> uploadEndorsementCheckFiles(@RequestBody UploadEndorsementCheckFile uploadEndorsementCheckFile) throws Exception {

                    log.error("电子批单绑定失败", cause);
                    return null;
                }

                @Override
                public GenericDataResponse<PolQueryForProDetailVO> queryLiabilityPolDetail(String policyNo) throws Exception {
                    log.error("责任险保单详情查询", cause);
                    return null;
                }

                @Override
                public GenericDataResponse submitEndors(String json) throws Exception {
                    log.error("提交批单申请失败", cause);
                    return null;
                }

                @Override
                public ResponseEntity<GenericResponse> endorsementRectify(EndorsementRectifyVO rectifyUpdateQo) {
                    log.error("批单冲正失败", cause);
                    return null;
                }

                @Override
                public GenericDataResponse<Map<String, Integer>> validEndorsementsNoUniq(String endorsementsNo, String channelCode) throws Exception {
                    log.error("校验渠道批单号唯一性", cause);
                    return null;
                }


            };
        }
    }
}
